<?php
class Entries_model extends CI_Model
{
	public function __construct()
	{
		$this->load->database();
	}	
	
	public function get_pupil_entries($pupil_id = FALSE)
	{
		$this->db->select('*');
		$this->db->from('entries');
		$this->db->join('pupils','pupils.id = entries.pupil_id');
		$this->db->join('users','users.id = entries.teacher_id');
		$this->db->join('rooms','rooms.name = entries.room');
		if ($pupil_id !== FALSE)
		{
			$this->db->where(array('entries.pupil_id' => $pupil_id));
		}
		$query = $this->db->get();
		return $query->result_array();
	}	

	public function get_room_entries($room_id = FALSE,$date = FALSE)
	{
		$this->db->select('pupils.id,pupils.name AS name,pupils.surname AS surname, pupils.class AS pupilclass, roll_call');
		$this->db->from('entries');
		$this->db->join('pupils','pupils.id = entries.pupil_id');
		$this->db->join('users','users.id = entries.teacher_id');
		$this->db->join('rooms','rooms.name = entries.room');
		if ($room_id !== FALSE)
		{
			$this->db->where(array('rooms.name' => $room_id));
		}
		if ($date == FALSE)
		{
			$date = date('Y-m-d');
		}
		$this->db->where(array('entries.date' => $date));
		$query = $this->db->get();
		return $query->result_array();
	}	

	public function set_entry($pupil_id,$room,$teacher_id,$date)
	{
		$data = array(
			'pupil_id' => $pupil_id,
			'room' => $room,
			'date' => $date,
			'teacher_id' => $teacher_id
		);
		
		return $this->db->insert('entries', $data);
	}	
	
	public function delete_entry($pupil_id,$room,$teacher_id,$date)
	{
		$data = array(
			'pupil_id' => $pupil_id,
			'room' => $room,
			'date' => $date
		);
		
		return $this->db->delete('entries', $data);
	}	

	public function roll_call($pupil_id,$room,$teacher_id,$date,$status)
	{
		$data = array(
			'roll_call' => $status,
			'roll_call_teacher_id' => $teacher_id
		);
		$this->db->where('pupil_id', $pupil_id);
		$this->db->where('room', $room);		
		$this->db->where('date', $date);
		return $this->db->update('entries', $data);
	}	
	


}